var cf = require('../lib/config.js');
var c = require('../lib/common.js');
var time = require('../lib/time-set.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pname: '立即送货',
    total_price: 0,
    price: 0,
    index: 0,
    arraykg: ['1', '5', '10', '20', '30', '40'],
    array: ['1kg', '5kg', '10kg', '20kg', '30kg', '40kg'],
    indexzhongliang: 0,
    quaddress: '必须告诉他取货地址',
    huoddress: '必须告诉他收货地址',
    awary: '0km',
    dbimgurl: '/images/tu.png',
    weight: 1,
    money: 0,
    //保价param
    distance: 0,//距离
    weight: 0,//总量
    add_money: 0,//我要买加的钱
    is_insureds: 0,//是否是保价
    insureds: 0,//保价的金额
    baostatus: 0,//开关
    baovalue: '点击右方按钮进行保价',
    baoselect: '/images/select.png',
    //保价param
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var selecttimen = parseInt(Number(new Date() / 1000));//当天时间戳
    this.setData({ selecttimen: selecttimen })
    wx.showLoading({
      title: '加载中....',
      mask: true,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
    time.settime(this, this.data.pname);
    var id = options.id;
    var that = this;
    console.log(id);
    that.setData({ id: id })
    c.ajax(
      cf.hostapi + 'getMenuListInfo', {
        cateid: id
      },
      res => {
        wx.hideLoading();
        var datall = res.data.data;
        for (var i = 0; i < datall.length; i++) {
          if ([i] == 0) {
            datall[i]['action'] = 'active';
            that.setData({ tid: datall[i]['id'] })
          } else {
            datall[i]['action'] = 'no';
          }
        }
        this.setData({
          data: datall
        })
      }
    )
  },
  previewImage: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var quid = this.data.quid;
    if (that.data.state) {
      console.log(this.data);
      if (this.data.type == 1) { //取
        c.toAddressIdGetInfo(quid, that, 'qu','money');
      }
      if (this.data.type == 2) { //收
        c.toAddressIdGetInfo(quid, that, 'huo', 'money');
      }
    }
  },
  
  //保价开关按钮
  baoclick() {
    c.baoclick(this);
  },
  //保价框输入金额
  baojiainput(e) {
    this.setData({ insureds: e.detail.value })
    c.getAmountInfo(this);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //选择二级菜单选中
  clicktop: function (e) {
    var datall = this.data.data;
    var tid = e.currentTarget.dataset.tid;
    for (var i = 0; i < datall.length; i++) {
      if (datall[i]['id'] == tid) {
        datall[i]['action'] = 'active';
      } else {
        datall[i]['action'] = 'no';
      }
    }
    console.log(datall);
    this.setData({
      data: datall, tid: tid
    })
  },
  //选择重量 
  bindPickerChange: function (e) {
    var arraykg = this.data.arraykg;
    this.setData({
      indexzhongliang: e.detail.value,
      weight: arraykg[e.detail.value]
    })
    console.log(arraykg[e.detail.value]);
    c.getAmountInfo(this);
  },
  addressinfo: function (e) {
    var type = e.currentTarget.dataset.type;
    this.setData({
      type: type
    })
    wx.navigateTo({
      url: '/pages/address/address?page=2',
    })
  },

  beizhu: function (e) {
    var value = e.detail.value;
    this.setData({ beizhu: value })
  },
  //图片上传
  tuup: function () {
    c.tuup(this);
  },
  //取货时间确定
  bindMultiPickerChange(e) {
    time.bindMultiPickerChange(e, this);
  },
  //获取时间选择
  bindMultiPickerColumnChange(e) {
    time.bindMultiPickerColumnChange(e, this, this.data.pname);
  },
  //确定
  submit: function () {
    wx.showLoading({
      title: '提交订单...',
    })
    var integral = this.data.integral;
    var uid = wx.getStorageSync('userinfo').id;
    var selecttime = this.data.selecttime;
    time.setselecttime(selecttime, this);
    var selecttimen = this.data.selecttimen;
    var id = this.data.id;//一级菜单
    var tid = this.data.tid;//一级菜单
    var beizhu = this.data.beizhu == undefined ? '暂无' : this.data.beizhu; //备注
    var weight = this.data.weight;//重量
    var imgid = this.data.imgid == undefined ? '' : this.data.imgid;//图片id
    var huoddressinfo = this.data.huoddressinfo;//获取地址信息
    var quaddressinfo = this.data.quaddressinfo;
    var s = this.data.s;//千米
    var total_price = this.data.total_price;//价钱
    var price=this.data.price;
    var insureds = this.data.insureds_price;//保价
    var is_insureds = this.data.is_insureds;//是否是保价
    var insureds_price = this.data.insureds;//保价物品input输入的价钱
    if (typeof (huoddressinfo) == 'undefined') {
      wx.showToast({
        title: '请填写收货地址',
        image: '/images/closered.png'
      })
    }
    if (typeof (quaddressinfo) == 'undefined') {
      wx.showToast({
        title: '请填写取货地址',
        image: '/images/closered.png'
      })
    }
    console.log(id);
    console.log(tid);
    console.log(weight);
    console.log(imgid);
    console.log(huoddressinfo);
    console.log(quaddressinfo);
    console.log(s);
    console.log(total_price);
    console.log(price);
    console.log(selecttimen);
    console.log(quaddressinfo['details'] + 666);
    c.ajax(
      cf.hostapi + 'createOrder',
      { cate_1_id: id, cate_2_id: tid, ramark: beizhu, images: imgid, weight: weight, start_address: quaddressinfo['id'], end_address: huoddressinfo['id'], total_price: total_price, price: price, distance: s, statr_time: selecttimen, uid: uid, integral: integral, insureds: insureds, is_insureds: is_insureds, insureds_price: insureds_price },
      res => {
        var order_id = res.data.data;
        if (res.data.retcode == 200) {
          wx.hideLoading();
          wx.showToast({
            title: '生成订单成功',
            image: '/images/dui.png'
          })
          setTimeout(function () {
            wx.redirectTo({
              url: '/pages/paynow/paynow?order_id=' + order_id,
            })
          }, 1000)
        };
      }
    )
  }
})